package com.danco.training.zaneuskaya.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.danco.training.zaneuskaya.domain.Interest;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.InterestDao;
import com.danco.training.zaneuskaya.repository.UserDao;
import com.danco.training.zaneuskaya.service.InterestService;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Service("interestService")
@Transactional
public class InterestServiceImpl implements InterestService {

	private static final String INTEREST_NOT_FOUND = "Interest not found ";

	private static final Logger logger = Logger
			.getLogger(InterestServiceImpl.class);

	@Autowired
	private InterestDao interestDao;

	@Autowired
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public InterestDao getInterestDao() {
		return interestDao;
	}

	public void setInterestDao(InterestDao interestDao) {
		this.interestDao = interestDao;
	}

	@Override
	public void createEntity(Interest t)  {
		try {
			
			interestDao.create(t);
			
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void deleteEntityById(Integer id) {
		try {
			Interest interest = interestDao.read(id);
			interestDao.delete(interest);
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public void updateEntity(Interest t) {
		try {
			Interest interest = interestDao.read(t.getInterestId());
			interest.setInterestTitle(t.getInterestTitle());
			
			interestDao.update(interest);
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
	}

	@Override
	public Interest getEntityById(Integer id) {
		Interest interest = null;
		try {
			interest = interestDao.read(id);
			if (interest == null){
				throw new NullPointerException(INTEREST_NOT_FOUND);
			}
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return interest;
	}

	@Override
	public List<Interest> getEntityList() {
		List<Interest> interests = null;
		try {
			interests = interestDao.readAll();
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return interests;
	}

	@Override
	public List<Interest> getUserInterests(User self) {
		List<Interest> interests = null;
		try {

			self = userDao.read(self.getUserId());
			interests = new ArrayList<Interest>(self.getInterests());
		} catch (NullPointerException e) {
			logger.error(e.getMessage(), e);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
		}
		return interests;
	}

}
